package com.bee.user.client.impl;

import com.bee.user.client.UserClient;
import com.bee.user.client.dto.UserDTO;
import com.bee.user.client.enums.UserTypeEnum;
import com.bee.user.common.domain.UserDO;
import com.bee.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Damin.yang
 * @date 2019/08/23 14:17
 */
@RestController
public class UserClientImpl implements UserClient {

    @Autowired
    private UserService userService;

    @Override
    public UserDTO queryUser(String username) {
        UserDO userDO = userService.queryUser(username);
        if (userDO != null) {
            UserDTO dto = new UserDTO();
            BeanUtils.copyProperties(userDO, dto);

            if (dto.getType() == UserTypeEnum.MANAGEMENT.ordinal()) {
                List<String> roles = userService.getUserRoles(userDO.getId());
                dto.setRoles(roles);
            }

            return dto;
        }
        return null;
    }
}
